//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Core.Serialization;
using OpenLS.Vml.Drawing;
using OpenLS.Vml.Serialization;

namespace OpenLS.Vml
{
    class ShapeElementOrInk
    {
        public ShapeElement Element { get; set; }
        public Ink Ink { get; set; }

        public ShapeElementOrInk(Ink ink)
        {
            Ink = ink;
        }

        public ShapeElementOrInk(ShapeElement element)
        {
            Element = element;
        }
    }
    class Polyline : ShapeBase
    {
        private string _points;
        List<ShapeElementOrInk> _elements = new List<ShapeElementOrInk>();

        public override void ReadXml(ReadContext context)
        {
            using (var c = context.Read(VmlConstants.VmlNamespace + "polyline"))
            {
                _points = c.GetOptionalString("points");
                ReadShapeAttributes(c);
                while (c.ReaderLocalName != null)
                {
                    if (c.ReaderLocalName == "ink")
                        _elements.Add(new ShapeElementOrInk(c.ReadElement<Ink>()));
                    else
                    {
                        _elements.Add(new ShapeElementOrInk(ShapeElement.ReadShapeElement(c)));
                    }

                }
            }

        }

        public override void WriteXml(WriteContext context)
        {
            using (var c = context.Write(VmlConstants.VmlNamespace + "polyline"))
            {
                c.SetOptionalString("points", _points);
                WriteShapeAttributes(c);
                foreach (var elementOrInk in _elements)
                {
                    if (elementOrInk.Ink != null)
                        c.WriteElement(elementOrInk.Ink);
                    else
                    {
                        c.WriteElement(elementOrInk.Element);
                    }
                }
            }
        }
    }
}